package borge.web.teamspirit.client.page.team.panels;

import java.util.HashMap;

import borge.web.teamspirit.client.interfaces.DataProvider;
import borge.web.teamspirit.client.interfaces.PanelHandler;
import borge.web.teamspirit.client.interfaces.ServerCallbackHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;
import borge.web.teamspirit.client.page.shared.widgets.ShowDataWidget;
import borge.web.teamspirit.client.page.shared.widgets.ShowNewsWidget;
import borge.web.teamspirit.client.page.shared.widgets.ShowPersonWidget;
import borge.web.teamspirit.client.page.team.TokenData;
import borge.web.teamspirit.client.page.team.widgets.ShowMatchWidget;
import borge.web.teamspirit.client.page.team.widgets.ShowPlayerWidget;
import borge.web.teamspirit.client.page.team.widgets.ShowSerieWidget;
import borge.web.teamspirit.client.page.team.widgets.ShowTeamStatsWidget;
import borge.web.teamspirit.client.page.team.widgets.ShowTeamWidget;
import borge.web.teamspirit.client.servercomm.DBQueryResults;

import com.google.gwt.user.client.Window;

public class ShowPanel extends MiddlePanel implements WidgetEventHandler, ServerCallbackHandler{

    private DataProvider iDataProvider;
    private ShowDataWidget iActiveWidget;
    
    private HashMap<String, ShowDataWidget> iTokenMap;
    
    public ShowPanel(/*ServerComm aServerComm,*/ PanelHandler aPanelHandler){
    	super(/*aServerComm,*/ aPanelHandler);
    	
    	iTokenMap = new HashMap<String, ShowDataWidget>();
    }
    
    public void setDataProvider(DataProvider aProvider){
        iDataProvider = aProvider;
    }
    
    public void setActiveWidget(ShowDataWidget aWidget){
        if(iActiveWidget != null)
            remove(iActiveWidget);
        
        iActiveWidget = aWidget;
        iActiveWidget.setWidgetEventHandler(this);
        add(iActiveWidget);
    }
    
    public void handleWidgetEvent(WidgetEventType aEvent){
     
        switch(aEvent){
        }
        
    }
    
    public void handleServerCallback(DBQueryResults aResult, CallbackType aType){
        
    	switch(aType){
    	case RETRIEVED_DATA: {
    		
    		iActiveWidget.populateData(aResult);
    		
    		break;
    	}
    	}
    	
    }
    
    public void handleServerError(Throwable t){
        //Window.alert("Server error: " + t.toString());
        t.printStackTrace();
    }

    private ShowDataWidget createWidget(String aToken, String aData){
    	
    	ShowDataWidget widget = null;
    	if(aToken.equals(TokenData.PERSON)){
    		widget = new ShowPersonWidget();
    	}
    	else if(aToken.equals(TokenData.TEAM)){
    		widget = new ShowTeamWidget(this);
    	}
    	else if(aToken.equals(TokenData.SERIE)){
    		widget = new ShowSerieWidget(this);
    	}
    	else if(aToken.equals(TokenData.PLAYER)){
    		widget = new ShowPlayerWidget();
    	}
    	else if(aToken.equals(TokenData.MATCH)){
    		widget = new ShowMatchWidget(this, this);
    	}
    	else if(aToken.equals(TokenData.NEWS)){
    		widget = new ShowNewsWidget(this);
    	}
    	else if(aToken.equals(TokenData.STATISTICS)){
    		widget = new ShowTeamStatsWidget(this);
    	}
    	else
    		Window.alert("Unknown widget token: " + aToken);
    	return widget;
    	
    	
    }

	@Override
	public void forwardToken(String aWidget, String aData) {
		// TODO Auto-generated method stub
		
		ShowDataWidget widget = iTokenMap.get(aWidget);
		
		if(widget == null){
			widget = createWidget(aWidget, aData);
			// TODO: Handle widget null here or in create?
			iTokenMap.put(aWidget, widget);
		}
		
		if(widget.needsUpdate(aData)){
			widget.requestServerData(this);
		}
		
		setActiveWidget(widget);
		
		
	}

}
